package com.wwp.wkb.ui.common;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.wwp.wkb.ui.CalendarActivity;
import com.wwp.wkb.ui.DateActivity;
import com.wwp.wkb.ui.FocusActivity;
import com.wwp.wkb.ui.ImgviewActivity;
import com.wwp.wkb.ui.ImgviewActivity2;
import com.wwp.wkb.ui.ImgviewActivity3;
import com.wwp.wkb.ui.TabMainActivity;
import com.wwp.wkb.ui.TabTopActivity;
import com.wwp.wkb.ui.ToastActivity;
import com.wwp.wkb.ui.web.WebActivity;
import com.wwp.wkb.ui.web.WebContentActivity;
import com.wwp.wkb.ui.web.WebFileActivity;

import java.util.ArrayList;
import java.util.List;

public class Commons {
    public static List<CommonItem> getMain() {
        List list = new ArrayList();
        list.add(new CommonItem(TabMainActivity.class, "带图标的下tab"));
        list.add(new CommonItem(TabTopActivity.class, "上tab"));
        list.add(new CommonItem(CalendarActivity.class, "CalendarView"));
        list.add(new CommonItem(DateActivity.class, "DatePicker"));
        list.add(new CommonItem(WebActivity.class, "WebView url"));
        list.add(new CommonItem(WebFileActivity.class, "WebView file"));
        list.add(new CommonItem(WebContentActivity.class, "WebView content"));
        list.add(new CommonItem(ImgviewActivity.class, "Imgview scaleType -- 横向大图"));
        list.add(new CommonItem(ImgviewActivity2.class, "Imgview scaleType -- 竖向大图"));
        list.add(new CommonItem(ImgviewActivity3.class, "Imgview scaleType -- 小图"));
        list.add(new CommonItem(FocusActivity.class, "focus test"));
        list.add(new CommonItem(ToastActivity.class, "自定义toast"));
        return list;
    }

    public static CommonItemAdapter.OnItemClickListener getOnItemClickListener(final Activity activity) {
        return new CommonItemAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int pos, CommonItem data) {
                String title = data.getTitle();
                if (!TextUtils.isEmpty(title)) {
                    Toast.makeText(activity, title, Toast.LENGTH_SHORT).show();
                }

                Class clazz = data.getClazz();
                if (clazz != null) {
                    Intent intent = new Intent(activity, clazz);
                    if (title.equals("WebView url")) {
                        intent.putExtra("title", "标题");
                        intent.putExtra("url", "www.baidu.com");
                    }
                    activity.startActivity(intent);
                }
            }
        };
    }

    public static void showRecycleView(Activity activity, int resId, List<CommonItem> items) {
        showRecycleView(activity, resId, items, getOnItemClickListener(activity));
    }

    public static void showRecycleView(Activity activity, int resId, List<CommonItem> items, CommonItemAdapter.OnItemClickListener listener) {
        RecyclerView rvContent = activity.findViewById(resId);
        rvContent.setLayoutManager(new LinearLayoutManager(activity));
        CommonItemAdapter adapter = new CommonItemAdapter(activity, items);
        adapter.setOnItemClickListener(listener);
        rvContent.setAdapter(adapter);
    }
}
